#ifndef _COMMON_H_
#define _COMMON_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/soundcard.h>
#include <getopt.h>
#include <pthread.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <sys/poll.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <math.h>
#include <signal.h>
#include <linux/tcp.h>
#include <netinet/tcp.h>
#include <dirent.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if.h>

#include "mxml.h"
#include "Net.h"
#include "netProtocol.h"
#include "XmlParser.h"
#include "Base.h"
#include "Ipc.h"


#define LOCAL_RECORD_PATH "/mnt/sd"
#define LOCAL_ALARM_RECORD_PATH "/mnt/alarm"

#define HEARTCIRCLE 120
#define LOCAL_HDR_LEN 33

#define LOCAL_NET_VIDEO 0x1111
#define REMOTE_NET_VIDEO 0x2222
#define LOCAL_NET_VOICE 0x3333
#define REMOTE_NET_VOICE 0x4444
#define LOCAL_NET_MJPEG 0X5555
#define LOCAL_NET_MJPEG_CTRL 0X6666

#define LOCAL_NET_PORT 4607
//#define REMOTE_NET_PORT 4603
#define REGISTE_DATA_LEN 4096
#define DATETIME_LEN 19

#define VIDEO_PROTOCOL "Jiuzhou_MPEG4"
enum LOCAL_INIT_CONF{
    LOCAL_INITCONF = 0X0101
};
enum LOCAL_VIDEO{
    LOCAL_VIDEO_REQUEST = 0x0201,
    LOCAL_VIDEO_REQUEST_RES,
    LOCAL_VIDEO_BEGIN_TRANSMIT,
    LOCAL_VIDEO_BEGIN_TRANSMIT_RES,
    LOCAL_VIDEO_CLOSE,
    LOCAL_VIDEO_CLOSE_RES
};

enum LOCALCODECPRAR{
    LOCAL_CODEC_PARA_SETTING = 0x0207,
    LOCAL_CODEC_PARA_SETTING_RES,
    LOCAL_CODEC_PARA_GETTING,
    LOCAL_CODEC_PARA_GETTING_RES,
    LOCAL_CAPTURE_PARA_SETTING=0x024b,
    LOCAL_CAPTURE_PARA_SETTING_RES,
    LOCAL_CAPTURE_PARA_GETTING,
    LOCAL_CAPTURE_PARA_GETTING_RES
};

enum LOCALVOICECALL{
	LOCAL_VOICE_REQ = 0x020b,
	LOCAL_VOICE_REQ_RES,
	LOCAL_VOICE_SET_PARAM,
	LOCAL_VOICE_SET_PARAM_RES,
	LOCAL_VOICE_GET_PARAM,
	LOCAL_VOICE_GET_PARAM_RES
};

enum LOCAL_RECORD{
    LOCAL_RECORD_REQUEST = 0X0301,
    LOCAL_RECORD_REQUEST_RES,
    LOCAL_RECORD_QUERY = 0x0305,
    LOCAL_RECORD_QUERY_RES,
    LOCAL_RECORD_DOWNLOAD,
    LOCAL_RECORD_PLAY
};
enum LOCAL_SNAP{
    LOCAL_SNAP_QUERY = 0x0311,
    LOCAL_SNAP_QUERY_RES,
    LOCAL_SNAP_DOWNLOAD,
    LOCAL_SNAP_DOWNLOAD_RES
};

enum LOCAL_ALARM{
	LOCAL_ALARM_CONF = 0x0401,
	LOCAL_ALARM_LISTEN,
	LOCAL_ALARM_LISTEN_RES,
	LOCAL_ALARM_GET_CONF,
	LOCAL_ALARM_GET_CONF_RES
};

enum LOCALSCHEDULE{
    LOCAL_SCHEDULE_CONF = 0x0501,
	LOCAL_SCHEDULE_CONF_RES,
    LOCAL_SCHEDULE_GET_CONF,
	LOCAL_SCHEDULE_GET_CONF_RES,
	LOCAL_HDD_PLAN_CONF,
	LOCAL_HDD_PLAN_CONF_RES,
	LOCAL_HDD_PLAN_GET_CONF,
	LOCAL_HDD_PLAN_GET_CONF_RES
};

enum LOCALNETCONF{
    LOCAL_NET_CONF = 0x0701,
	LOCAL_NET_CONF_RES,
	LOCAL_NET_GET_CONF,
	LOCAL_NET_GET_CONF_RES,
	LOCAL_TM_CONF,
	LOCAL_TM_CONF_RES,
	LOCAL_TM_GET_CONF,
	LOCAL_TM_GET_CONF_RES,
	LOCAL_SD_GET_CONF,
	LOCAL_SD_GET_CONF_RES,
	LOCAL_SD_CONF,
	LOCAL_SD_CONF_RES
};

enum LOCALSNAPCONF{
	LOCAL_SNAP_CONF = 0x0801,
	LOCAL_SNAP_CONF_RES,
	LOCAL_SNAP_GET_CONF,
	LOCAL_SNAP_GET_CONF_RES
};
enum LOCALVOICECONF{
	LOCAL_VOICE_CONF = 0xFF01,
	LOCAL_VOICE_CONF_RES,
	LOCAL_VOICE_GET_CONF,
	LOCAL_VOICE_GET_CONF_RES
};



enum LOCAL_PTZ{
	LOCAL_PTZ_CONF =0X0901,
	LOCAL_PTZ_CONF_RES,
	LOCAL_PTZ_CONF_GET,
	LOCAL_PTZ_CONF_GET_RES,
	LOCAL_PTZ_COMMAND,
	LOCAL_PTZ_COMMAND_RES,
	LOCAL_PTZ_PRESET,
	LOCAL_PTZ_PRESET_RES,
	LOCAL_PTZ_CRUISE,
	LOCAL_PTZ_CRUISE_RES,
	LOCAL_PTZ_TRACE,
	LOCAL_PTZ_TRACE_RES,
	LOCAL_PTZ_LINERSCAN,
	LOCAL_PTZ_LINERSCAN_RES,
	LOCAL_PTZ_STATUS = 0x0910,
    LOCAL_PTZ_STATUS_RES,
};

#ifdef EX_SENSOR
enum LOCAL_SENSOR {
    LOCAL_SENSOR_TEMP = 0x0990,
    LOCAL_SENSOR_TEMP_RES,
};
#endif

enum DIAL_CONF
{
	DIALUP_CONF_SET = 0x0c05,
	DIALUP_CONF_SET_RES,
    DIALUP_CONF_GET,
    DIALUP_CONF_GET_RES,
	DIALUP_ST,
	DIALUP_ST_RES,
};
#define     DISABLE_WIRELESS_DIAL    0x00
#define     ENABLE_WIRELESS_DIAL     0x01
#define     DISABLE_PPPOE_DIAL      0x02
#define     ENABLE_PPPOE_DIAL       0x03

enum HUXINCONF{
   HUXIN_CONF_SET = 0x0c01,
   HUXIN_CONF_SET_RES,
   HUXIN_CONF_GET,
   HUXIN_CONF_GET_RES
};

enum LOCAL_SYSCONF {
	LOCAL_VERSION_GET_CONF = 0x0e01,
    LOCAL_VERSION_GET_CONF_RES
};

enum LOCAL_UPGRADE {
	LOCAL_UPGRADE_SET = 0x0d01,
    LOCAL_UPGRADE_SET_READY,
    LOCAL_UPGRADE_SET_RES
};

enum {
    ERR_CRC_HEAD    = 2,
    ERR_CRC_DATA    = 3,
    ERR_NO_IMG      = 4,
    ERR_INCORRECT_IMG = 5,
    ERR_FSEEK       = 16,      ///< fseek error
    ERR_FREAD       = 17,
    ERR_FWRITE      = 18,
};


#define REGISTER_MESSAGE 2002
#define REGISTER_MESSAGE_RES 2003
#define STORAGE_REG_MSG 4002

#define HEARTBEAT_MESSAGE 2
#define HEARTBEAT_MESSAGE_RES 3

#define AUTHOR_CODE 5010
#define AUTHOR_CODE2 6002
#define AUTHOR_RES_CODE 5011
#define AUTHOR_RES_CODE2 6003

#define VIDEO_ORIGINAL_CONNECT 4
#define VIDEO_ORIGINAL_CONNECT_RES 5
#define VIDEO_CONNECT_MESSAGE 8000
#define VIDEO_CONNECT_RES_MESSAGE 8001
#define VIDEO_CONNECT_PRIVATE_MESSAGE 8002
#define VIDEO_CLOSE_CONNECT_MESSAGE 2012
#define VIDEO_CLOSE_CONNECT_MESSAGE_RES 2013

enum ALARM_OUTPUT{
	SET_ALARM_OUTPUT_STATE = 0x13c8,
	SET_ALARM_OUTPUT_STATE_RES,
	GET_ALARM_OUTPUT_STATE,
	GET_ALARM_OUTPUT_STATE_RES,
};

enum ALARM_PLAN{
	SET_ALARM_PLAN = 0x13d8,
	SET_ALARM_PLAN_RES,
	GET_ALARM_PLAN,
	GET_ALARM_PLAN_RES
};

enum ALARM_NOTICE{
	ALARM_NOTICE_MSG = 0x145e,
	ALARM_NOTICE_MSG_RES,
	SET_SCHEME_ASSOCIATE = 0X1458,
	SET_SCHEME_ASSOCIATE_RES,
	DEL_SCHEME_ASSOCIATE,
	DEL_SCHEME_ASSOCIATE_RES
};

enum VIDEO_PARA{
    VIDEO_PARA_SETTING = 5032,
    VIDEO_PARA_SETTING_RES,
    VIDEO_PARA_GETTING,
    VIDEO_PARA_GETTING_RES
};

enum VIDEO_RECORD{
    VIDEO_RECORD_REQUEST = 5004,
    VIDEO_RECORD_REQUEST_RES
};

enum VOICE_CALL{
    VOICE_CALL_AUTH = 6110,
    VOICE_CALL_AUTH_RES,
    VOICE_CALL_CONNECT,
    VOICE_CALL_CONNECT_RES
};

enum VIDEO_RECORD_DOWNLOAD{
    DOWNLOAD_VIDEO_RECORD = 0x206c,
    DOWNLOAD_VIDEO_RECORD2 = 0x206e,
    DOWNLOAD_VIDEO_RECORD_RES
};

/* used for auto search local IP info */
#define LOCAL_UDP_PORT  5050
enum AUTO_SEARCH_IP {
    AUTO_SEARCH_IP_REQUEST = 0x0cEE,
    AUTO_SEARCH_IP_REQUEST_RES
};

#define SWITCH_OPEN_MODE 0x1
#define SWITCH_CLOSE_MODE 0x2

enum VSP_XML{
    VSP_SEND_XML = 110,
    VSP_RECV_XML,
    VSP_OTHER_XML
};

#define CONNECT_VALID 8000



#define OPEN_FILE(file, mode) (fopen(file, mode))

int InitSocket(char *ip, int port);

int SendDataFunc(char *buffer, int bufLen, int hdrLen, FILE *fp, int fd);
VSP_SNDHEADER InitVSPHeader(int len, int code, int seq, char *srcId, char *dstId);
int GetCmdCode(char *buffer);
mxml_node_t* ParserXML(char *buffer, int len, int flags);

void itoa(int value, char *str, int strLen);
void InitLocalProHdr(char *buff, int code, int len);
void SendIpcMsg(char *buff, int blen, int src_proc, int dst_proc, int type);
void SendNetData(void* args, int flag,char* cmd_buff,int cmd_len);

#endif


